package com.huluobo.a2110mvpimdemo.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2110mvpimdemo.R;
import com.huluobo.a2110mvpimdemo.chat.ChatActivity;
import com.huluobo.a2110mvpimdemo.chat.MyChatActivity;
import com.huluobo.a2110mvpimdemo.contact.ContactListItemBean;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import java.util.List;

/**
 * Created by LC on 2023/12/19.
 */
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
    private List<ContactListItemBean> listItemBeans;
    private Activity context;

    public ContactAdapter(List<ContactListItemBean> listItemBeans, Activity context) {
        this.listItemBeans = listItemBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        if (listItemBeans.get(position).isShowFirstLetter()) {
            holder.firstLetter.setVisibility(View.VISIBLE);
            holder.firstLetter.setText(listItemBeans.get(position).getFirstLetter() + "");
        } else {
            holder.firstLetter.setVisibility(View.GONE);
        }
        holder.username.setText(listItemBeans.get(position).getUsername());

        holder.layout.setOnClickListener(v -> {
            String conversationId = listItemBeans.get(position).getUsername();
            Intent intent = new Intent(context, MyChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", 1);
            intent.putExtras(bundle);
            context.startActivity(intent);
        });

        holder.layout.setOnLongClickListener(v -> {
            String message = String.format("要与%s友尽吗?", listItemBeans.get(position).getUsername());
            new AlertDialog.Builder(context)
                    .setTitle("删除好友")//弹窗标题
                    .setNegativeButton("取消", null)//设置取消按钮文本和点击事件
                    .setMessage(message)//弹窗内容
                    .setPositiveButton(
                            "确认", (dialog, which) -> deleteFriend(listItemBeans.get(position).getUsername())
                            //设置确认按钮点击事件
                    ).show();//展示弹窗
            return true;
        });
    }

    private void deleteFriend(String username) {
        EMClient.getInstance().contactManager().aysncDeleteContact(username, new EMCallBack() {
            @Override
            public void onSuccess() {
                context.runOnUiThread(() -> {
                    Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    notifyDataSetChanged();
                });

            }

            @Override
            public void onError(int code, String error) {
                context.runOnUiThread(() -> Toast.makeText(context, "删除失败:" + error, Toast.LENGTH_SHORT).show());
            }
        });
    }

    @Override
    public int getItemCount() {
        return listItemBeans.size();
    }

    static class ContactViewHolder extends RecyclerView.ViewHolder {
        TextView username, firstLetter;
        LinearLayout layout;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.item_contact_username);
            firstLetter = itemView.findViewById(R.id.item_contact_firstLetter);
            layout = itemView.findViewById(R.id.item_contact_ll);
        }
    }
}
